import xadmin
from xadmin.plugins.auth import UserAdmin
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import (UserCreationForm, UserChangeForm,
                                       AdminPasswordChangeForm, PasswordChangeForm)
from crispy_forms.helper import FormHelper
from xadmin.layout import Fieldset, Main, Side, Row, Col, Container, InputGroup, PrependedAppendedText, AppendedText, \
    PrependedText, ButtonHolder, Submit, Div, TabHolder, Tab, InlineCheckboxes, InlineField
from .models import *
from .resources import *


# class ××××Admin(object):
#     # xadmin plugin built-in plugin
#     hidden_menu = True
#     action = []  # 继承BaseActionView定制action
#     list_bookmarks = [{
#         'title': "Female",  # 书签的名称, 显示在书签菜单中
#         'query': {'gender': True},  # 过滤参数, 是标准的 queryset 过滤
#         'order': ('-age'),  # 排序参数
#         'cols': ('first_name', 'age', 'phones'),  # 显示的列
#         'search': 'Tom'  # 搜索参数, 指定搜索的内容
#     },]
#     data_charts = {
#         "user_count": {'title': u"User Report", "x-field": "date", "y-field": ("user_count", "view_count"),
#                        "order": ('date',)},
#         "avg_count": {'title': u"Avg Report", "x-field": "date", "y-field": ('avg_count',), "order": ('date',)}
#     }  # flot.js orderdict
#     show_detail_fields = []
#     list_display = []
#     searchfields = []  # 搜索该类的外键字段 fk__attribute student__name
#     list_filter = []
#     ordering = ["-"]  # 默认排序方式 点击字段都可正序/倒序排列
#     free_query_filter = True  # http://xxx.com/xadmin/auth/user/?name__contains=tony
#     readonly_fields = []
#     exclude = [] # include相对，与readonly_fields冲突
#     relfield_style = "fk-ajax"  #  作为其他类引用的外键不再是直接下拉显示全部对象的__str__方法返回的字符串，而是显示含搜索内容的字段
#     list_editable = []  # 可以在列表页中直接编辑
#     inlines = []  # 引用该类的类，本类作为外键，外键为一，一对多
#     refresh_times = [3,5]  # 单位秒
# xadmin.site.register(××××,××××Admin)

class UserProfileAdmin(UserAdmin):
    # refresh_times = [3, 5]
    style_fields = {'user_permissions': 'm2m_transfer', "info": "ueditor"}
    model_icon = 'fa fa-user-o'
    readonly_fields = ['type']
    list_export = []
    import_export_args = {'import_resource_class': UserProfileResource, 'export_resource_class': UserProfileResource}
    hidden_menu = True
    def get_form_layout(self):
        if self.org_obj:
            self.form_layout = (
                Main(
                    Fieldset(
                        'ACCOUNT',
                        'username',
                        'password',
                        'type',
                        # css_class='unsort no_title'
                    ),
                    Fieldset(
                        'PERSONAL INFO',
                        'email',
                        Row('first_name', 'last_name'),
                        'gender',
                        'birth',
                        'nickname',
                        'mobile',
                        'wechat'

                    ),
                    Fieldset(
                        'PERMISSIONS',
                        'groups',
                        'user_permissions'
                    ),
                    Fieldset(
                        'IMPORTANT DATES',
                        'last_login',
                        'date_joined',
                        'created',
                        'modified'
                    ),
                    Fieldset(
                        'REMARKS',
                        'info'
                    )
                ),
                Side(
                    Fieldset(
                        'STATUS',  # first arg is the legend of the fieldset
                        'is_active',
                        'is_staff',
                        'is_superuser',
                    )
                ),
            )
        return super(UserAdmin, self).get_form_layout()

    # def queryset(self):
    #     return super(UserProfileAdmin, self).queryset().filter(type='00')


class AdministratorAdmin(UserAdmin):
    # refresh_times = [3, 5]
    style_fields = {'user_permissions': 'm2m_transfer', "info": "ueditor"}
    model_icon = 'fa fa-user-o'
    readonly_fields = ['type']
    list_export = []
    import_export_args = {'import_resource_class': UserProfileResource, 'export_resource_class': UserProfileResource}
    hidden_menu = True
    def get_form_layout(self):
        if self.org_obj:
            self.form_layout = (
                Main(
                    Fieldset(
                        'ACCOUNT',
                        'username',
                        'password',
                        'type',
                        # css_class='unsort no_title'
                    ),
                    Fieldset(
                        'PERSONAL INFO',
                        'email',
                        Row('first_name', 'last_name'),
                        'gender',
                        'birth',
                        'nickname',
                        'mobile',
                        'wechat'

                    ),
                    Fieldset(
                        'PERMISSIONS',
                        'groups',
                        'user_permissions'
                    ),
                    Fieldset(
                        'IMPORTANT DATES',
                        'last_login',
                        'date_joined',
                        'created',
                        'modified'
                    ),
                    Fieldset(
                        'REMARKS',
                        'info'
                    )
                ),
                Side(
                    Fieldset(
                        'STATUS',  # first arg is the legend of the fieldset
                        'is_active',
                        'is_staff',
                        'is_superuser',
                    )
                ),
            )
        return super(UserAdmin, self).get_form_layout()

    def queryset(self):
        return super(AdministratorAdmin, self).queryset().filter(type='00')


class ClerkAdmin(UserAdmin):
    # model_icon = 'fa fa-user-o'
    # refresh_times = [3, 5]
    change_user_password_template = None
    list_display = [field.name for field in Clerk._meta.fields]
    list_display_links = [field.name for field in Clerk._meta.fields]
    style_fields = {'user_permissions': 'm2m_transfer', "info": "ueditor"}
    import_export_args = {'import_resource_class': ClerkResource, 'export_resource_class': ClerkResource}
    list_export = []

    def get_form_layout(self):
        if self.org_obj:
            self.form_layout = (
                Main(
                    Fieldset(
                        'ACCOUNT',
                        'username',
                        'password',
                        'type',
                        # css_class='unsort no_title'
                    ),
                    Fieldset(
                        'CLERK INFO',  # first arg is the legend of the fieldset
                        'identity',
                        'base_pay',
                        'commission',
                        'fk_store',
                    ),
                    Fieldset(
                        'PERSONAL INFO',
                        'email',
                        Row('first_name', 'last_name'),
                        'gender',
                        'birth',
                        'nickname',
                        'mobile',
                        'wechat'

                    ),
                    Fieldset(
                        'PERMISSIONS',
                        'groups',
                        'user_permissions'
                    ),
                    Fieldset(
                        'IMPORTANT DATES',
                        'last_login',
                        'date_joined',
                        'created',
                        'modified'
                    ),
                    Fieldset(
                        'REMARKS',
                        'info'
                    )
                ),
                Side(
                    Fieldset(
                        'STATUS',  # first arg is the legend of the fieldset
                        'is_active',
                        'is_staff',
                        'is_superuser',
                    )
                ),
            )

        return super(UserAdmin, self).get_form_layout()

    def post(self, request, *args, **kwargs):
        # 当启用import_export_args时，可以在这里编写处理上传文件的代码
        return super(ClerkAdmin, self).post(request, *args, **kwargs)


class CustomerAdmin(UserAdmin):
    # list_display = [field.name for field in Sale._meta.fields]
    list_display = [field.name for field in Customer._meta.fields]
    list_display_links = [field.name for field in Customer._meta.fields]
    style_fields = {'user_permissions': 'm2m_transfer', "info": "ueditor"}
    import_export_args = {'import_resource_class': CustomerResource, 'export_resource_class': CustomerResource}
    list_export = []

    def get_form_layout(self):
        if self.org_obj:
            self.form_layout = (
                Main(
                    Fieldset(
                        'ACCOUNT',
                        'username',
                        'password',
                        'type',
                        # css_class='unsort no_title'
                    ),
                    Fieldset(
                        'CUSTOMER INFO',
                        'point',
                        'rank',
                    ),
                    Fieldset(
                        'PERSONAL INFO',
                        'email',
                        Row('first_name', 'last_name'),
                        'gender',
                        'birth',
                        'nickname',
                        'mobile',
                        'wechat'

                    ),
                    Fieldset(
                        'PERMISSIONS',
                        'groups',
                        'user_permissions'
                    ),
                    Fieldset(
                        'IMPORTANT DATES',
                        'last_login',
                        'date_joined',
                        'created',
                        'modified'
                    ),
                    Fieldset(
                        'REMARKS',
                        'info'

                    ),
                ),
                Side(
                    Fieldset(
                        'STATUS',  # first arg is the legend of the fieldset
                        'is_active',
                        'is_staff',
                        'is_superuser',
                    ),
                ),
            )

        return super(UserAdmin, self).get_form_layout()


User = get_user_model()
xadmin.site.unregister(User)
xadmin.site.register(User, UserProfileAdmin)
xadmin.site.register(Clerk, ClerkAdmin)
xadmin.site.register(Customer, CustomerAdmin)
xadmin.site.register(Administrator, AdministratorAdmin)

